package info.niwota.commons.web;

import info.niwota.commons.web.PageColumns.ColumnIndice;
import android.database.AbstractCursor;
import android.database.CursorIndexOutOfBoundsException;

/**
 * @author qiangli
 * 
 */
public class PageCursor extends AbstractCursor {
	public static interface Type {
		public static final int CONTENT = 0;
		public static final int URI = 1;
		public static final int REDIRECT = 2;
		public static final int ERROR = -1;
	}

	public long id = 0;
	public int type = 0;
	public String title;
	public String uri;
	public String content;
	public String contentEncoding;
	public String contentType = "text/html";
	public long contentLength;
	public long lastModified;
	public String icon;
	public String cookies;
	public String session;
	public String statusCode;

	private int _count = 1;
	
	public PageCursor() {
	}

	protected Object get(int column) {
		switch(column) {
		case ColumnIndice._ID:
			return id;
		case ColumnIndice.TYPE:
			return type;
		case ColumnIndice.TITLE:
			return title;
		case ColumnIndice.URI:
			return uri;
		case ColumnIndice.CONTENT:
			return content;
		case ColumnIndice.CONTENT_ENCODING:
			return contentEncoding;
		case ColumnIndice.CONTENT_TYPE:
			return contentType;
		case ColumnIndice.CONTENT_LENGTH:
			return contentLength;
		case ColumnIndice.LAST_MODIFIED:
			return lastModified;
		case ColumnIndice.ICON:
			return icon;
		case ColumnIndice.COOKIES:
			return cookies;
		case ColumnIndice.SESSION:
			return session;
		case ColumnIndice.STATUS_CODE:
			return statusCode;
		}
		throw new CursorIndexOutOfBoundsException("column: " + column);
	}

	@Override
	public String[] getColumnNames() {
		return PageColumns.ColumnNames;
	}
	
	@Override
	public int getCount() {
		return _count ;
	}

	public double getDouble(int column) {
		Object value = get(column);
		return (value instanceof String) ? Double.valueOf((String) value)
				: ((Number) value).doubleValue();
	}

	public float getFloat(int column) {
		Object value = get(column);
		return (value instanceof String) ? Float.valueOf((String) value)
				: ((Number) value).floatValue();
	}

	public int getInt(int column) {
		Object value = get(column);
		return (value instanceof String) ? Integer.valueOf((String) value)
				: ((Number) value).intValue();
	}

	public long getLong(int column) {
		Object value = get(column);
		return (value instanceof String) ? Long.valueOf((String) value)
				: ((Number) value).longValue();
	}

	public short getShort(int column) {
		Object value = get(column);
		return (value instanceof String) ? Short.valueOf((String) value)
				: ((Number) value).shortValue();
	}

	public String getString(int column) {
		Object o = get(column); 
		return (o == null ? null : String.valueOf(o));
	}

	public boolean isNull(int column) {
		return (get(column) == null);
	}

	@Override
	public boolean requery() {
		return true;
	}

	@Override
	public String toString() {
		return "PageCursor [content=" + (content == null ? "0" : content.length() + " bytes" ) + ", contentEncoding="
				+ contentEncoding + ", contentLength=" + contentLength
				+ ", contentType=" + contentType + ", cookies=" + cookies
				+ ", icon=" + icon + ", id=" + id + ", lastModified="
				+ lastModified + ", session=" + session + ", statusCode="
				+ statusCode + ", title=" + title + ", type=" + type + ", uri="
				+ uri + "]";
	}

}
